<template>
  <view class="following-container">
    <!-- 导航栏 -->
    <view class="nav-bar">
      <back-button></back-button>
      <text class="nav-title">我的关注</text>
      <view class="nav-right"></view>
    </view>

    <!-- 关注列表 -->
    <scroll-view 
      class="content-scroll" 
      scroll-y 
      @scrolltolower="loadMore"
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
    >
      <view class="user-list" v-if="followingList.length > 0">
        <view 
          class="user-item"
          v-for="user in followingList"
          :key="user.id"
        >
          <!-- 用户信息 -->
          <view class="user-info" @tap="goToUserSpace(user)">
            <image class="avatar" :src="user.avatar || '/static/images/default-avatar.jpg'" mode="aspectFill"></image>
            <view class="info">
              <text class="nickname">{{ user.nickname }}</text>
              <text class="signature">{{ user.signature || '这个人很懒，什么都没留下' }}</text>
            </view>
          </view>
          
          <!-- 操作按钮 -->
          <button 
            class="follow-btn following"
            @tap="handleUnfollow(user)"
          >
            已关注
          </button>
        </view>
      </view>

      <!-- 空状态 -->
      <view class="empty-state" v-else>
        <image src="/static/images/empty-follow.png" mode="aspectFit"></image>
        <text>暂无关注</text>
      </view>

      <!-- 加载更多 -->
      <uni-load-more :status="loadMoreStatus" v-if="followingList.length > 0"></uni-load-more>
    </scroll-view>
  </view>
</template>

<script>
import BackButton from '@/components/common/BackButton'
import { checkLogin } from '@/utils/auth'

export default {
  components: {
    BackButton
  },
  
  data() {
    return {
      followingList: [],
      page: 1,
      pageSize: 20,
      loadMoreStatus: 'more',
      isRefreshing: false
    }
  },

  onLoad() {
    if (!checkLogin()) {
      uni.navigateTo({
        url: '/pages/user/auth/login'
      })
      return
    }
    this.loadFollowingList()
  },

  methods: {
    // 加载关注列表
    async loadFollowingList() {
      try {
        if (this.page === 1) {
          this.followingList = []
        }
        
        this.loadMoreStatus = 'loading'
        
        const params = {
          pageNum: this.page,
          pageSize: this.pageSize
        }
        
        const res = await this.$api.community.follow.getFollowingList(params)
        
        if (res.code === 200 && res.data) {
          // 处理数据
          const newList = (res.data.records || []).map(user => ({
            ...user,
            isFollowing: true // 关注列表中的用户都是已关注的
          }))
          
          this.followingList = [...this.followingList, ...newList]
          this.loadMoreStatus = this.followingList.length >= res.data.total ? 'noMore' : 'more'
        } else {
          throw new Error(res.message || '加载失败')
        }
      } catch (error) {
        console.error('加载关注列表失败:', error)
        this.loadMoreStatus = 'more'
        uni.showToast({
          title: error.message || '加载失败',
          icon: 'none'
        })
      }
    },

    // 下拉刷新
    async onRefresh() {
      this.isRefreshing = true
      this.page = 1
      await this.loadFollowingList()
      this.isRefreshing = false
    },

    // 加载更多
    loadMore() {
      if (this.loadMoreStatus === 'more') {
        this.page++
        this.loadFollowingList()
      }
    },

    // 处理取消关注
    async handleUnfollow(user) {
      try {
        const res = await this.$api.community.follow.unfollowUser(user.id)
        if (res.code === 200) {
          // 从列表中移除该用户
          const index = this.followingList.findIndex(item => item.id === user.id)
          if (index !== -1) {
            this.followingList.splice(index, 1)
          }
          
          uni.showToast({
            title: '已取消关注',
            icon: 'success'
          })
        } else {
          throw new Error(res.message || '操作失败')
        }
      } catch (error) {
        console.error('取消关注失败:', error)
        uni.showToast({
          title: error.message || '操作失败',
          icon: 'none'
        })
      }
    },

    // 跳转到用户空间
    goToUserSpace(user) {
      uni.navigateTo({
        url: `/pages/community/user/index?id=${user.id}`
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.following-container {
  min-height: 100vh;
  background-color: #f8f8f8;
}

.nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 88rpx;
  background: #fff;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  z-index: 100;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  
  .nav-title {
    flex: 1;
    font-size: 32rpx;
    color: #333;
    font-weight: 500;
    text-align: center;
  }
  
  .nav-right {
    width: 88rpx;
  }
}

.content-scroll {
  height: calc(100vh - 88rpx);
  margin-top: 88rpx;
}

.user-list {
  padding: 20rpx;
  
  .user-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #fff;
    padding: 20rpx;
    margin-bottom: 20rpx;
    border-radius: 12rpx;
    
    .user-info {
      flex: 1;
      display: flex;
      align-items: center;
      margin-right: 20rpx;
      
      .avatar {
        width: 88rpx;
        height: 88rpx;
        border-radius: 50%;
        margin-right: 20rpx;
      }
      
      .info {
        flex: 1;
        
        .nickname {
          font-size: 28rpx;
          color: #333;
          font-weight: 500;
          margin-bottom: 8rpx;
          display: block;
        }
        
        .signature {
          font-size: 24rpx;
          color: #999;
          @include text-ellipsis;
        }
      }
    }
    
    .follow-btn {
      min-width: 140rpx;
      height: 56rpx;
      line-height: 56rpx;
      font-size: 24rpx;
      color: #fff;
      background: $uni-primary;
      border-radius: 28rpx;
      padding: 0 30rpx;
      margin: 0;
      
      &.following {
        color: #666;
        background: #f5f5f5;
      }
      
      &:active {
        opacity: 0.8;
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100rpx 0;
  
  image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }
  
  text {
    font-size: 28rpx;
    color: #999;
  }
}
</style> 